<template>
  <el-container class="panel-block">
    <el-header class="sa-header">
      <el-tabs class="sa-tabs" v-model="filterParams.data.tabsType" @tab-change="onChangeTabsType">
        <el-tab-pane
          v-for="item in tabsList"
          :key="item"
          :label="item.name"
          :name="item.type"
        ></el-tab-pane>
      </el-tabs>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">分销商管理</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="state.loading">
      <all-table v-if="filterParams.data.tabsType == 'all'" :data="state.tableData"></all-table>
      <pending-table
        v-else-if="filterParams.data.tabsType == 'pending'"
        :data="state.tableData"
        @updateList="getData"
      ></pending-table>
      <upgrade-table
        v-else-if="filterParams.data.tabsType == '0'"
        :data="state.tableData"
        @updateList="getData"
      ></upgrade-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.commission.agent',
  };
</script>
<script setup>
  import { onMounted, reactive } from 'vue';
  import { api } from '../commission.service';
  import { usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import PendingTable from './components/pending.vue';
  import AllTable from './components/all.vue';
  import UpgradeTable from './components/upgrade.vue';
  import { cloneDeep } from 'lodash';

  const tabsList = [
    {
      type: 'all',
      name: '分销商',
    },
    {
      type: 'pending',
      name: '待审核',
    },
    {
      type: '0',
      name: '待升级',
    },
  ];

  const filterParams = reactive({
    tools: {
      tabsType: { label: '状态', value: 'all' },
      user: {
        type: 'tinputprepend',
        label: '会员信息',
        placeholder: '请输入查询内容',
        field: 'user',
        user: {
          field: 'user_id',
          value: '',
        },
        options: [
          {
            label: '用户ID',
            value: 'user_id',
          },
          {
            label: '用户昵称',
            value: 'user.nickname',
          },
          {
            label: '用户手机号',
            value: 'user.mobile',
          },
        ],
      },
      level: {
        type: 'tselect',
        label: '分销商等级',
        field: 'level',
        value: '',
        options: {
          data: [],
          props: {
            label: 'name',
            value: 'level',
          },
        },
      },
      status: {
        type: 'tselect',
        label: '审核状态',
        field: 'status',
        value: '',
        options: {
          data: [
            {
              value: 'normal',
              label: '正常',
            },
            {
              value: 'forbidden',
              label: '禁用',
            },
            {
              value: 'pending',
              label: '审核中',
            },
            {
              value: 'freeze',
              label: '冻结',
            },
            {
              value: 'reject',
              label: '拒绝',
            },
          ],
        },
      },
      create_time: {
        type: 'tdatetimerange',
        label: '更新时间',
        field: 'create_time',
        value: [],
      },
    },
    data: {
      tabsType: 'all',
      user: { field: 'user_id', value: '' },
      level: '',
      status: '',
      create_time: [],
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });

  const state = reactive({
    loading: false,
    tableData: [],
  });

  const { pageData } = usePagination();

  // 获取数据
  async function getData(page) {
    state.loading = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch, {
      'user.nickname': 'like',
      'user.mobile': 'like',
      create_time: 'range',
    });
    let temp = JSON.parse(search.search);
    if (temp.tabsType) {
      if (temp.tabsType[0] == 'pending') {
        temp.status = ['pending', '='];
        delete temp.tabsType;
      } else if (temp.tabsType[0] == '0') {
        temp.level_status = ['0', '>'];
        delete temp.tabsType;
      }
    }
    search = { search: JSON.stringify(temp) };
    const { error, data } = await api.agent.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      ...search,
    });
    if (error === 0) {
      state.tableData = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    state.loading = false;
  }

  async function getLevel() {
    const { data } = await api.level.select();
    filterParams.tools.level.options.data = data;
  }

  function onChangeTabsType() {
    state.tableData = [];
    getData(1);
  }

  onMounted(() => {
    getData();
    getLevel();
  });
</script>
